﻿namespace Home.Repository
{
    using System;
    using System.Collections.Generic;
    using System.Data.Entity;
    using System.Data.Entity.ModelConfiguration;
    using System.Linq;
    using System.Web;
    using Home.Data;
    using Home.Models;

    public class ProductDbContext : EntityFrameworkDbContext
    {
        static ProductDbContext()
        {
            Database.SetInitializer<ProductDbContext>(null);
        }

        public ProductDbContext(bool enableTransaction = false)
            : base("Name=DefaultConnection", enableTransaction)
        {
        }

        public DbSet<ProductUpload> Uploads { get; set; }

        public DbSet<Diamond> Diamonds { get; set; }

        protected override void OnModelCreating(DbModelBuilder builder)
        {
            {
                var entity = builder.CreateEntity<ProductUpload>("t_product_upload");

                entity.Property(m => m.Count)
                      .HasColumnName("count")
                      .IsRequired();

                entity.Property(m => m.UploadType)
                      .HasColumnName("upload_type")
                      .IsRequired();

                entity.Property(m => m.ProductType)
                      .HasColumnName("product_type")
                      .IsRequired();

                entity.Property(m => m.RequestId)
                      .HasColumnName("request_id")
                      .IsRequired();
            }

            {
                var entity = builder.CreateEntity<Diamond>("t_product_diamond");
                InitializeProduct(entity);

                entity.Property(m => m.Type)
                      .HasColumnName("type")
                      .IsRequired();

                entity.Property(m => m.Shape)
                      .HasColumnName("shape")
                      .IsOptional();

                entity.Property(m => m.ReportType)
                      .HasColumnName("report_type")
                      .IsOptional();

                entity.Property(m => m.ReportNumber)
                      .HasColumnName("report_number")
                      .IsOptional()
                      .HasMaxLength(128)
                      .IsUnicode(true)
                      .IsVariableLength();

                entity.Property(m => m.Caret)
                      .HasColumnName("caret")
                      .IsOptional()
                      .HasPrecision(18, 2);

                entity.Property(m => m.Clarity)
                      .HasColumnName("clarity")
                      .IsOptional();

                entity.Property(m => m.Color)
                      .HasColumnName("color")
                      .IsOptional();

                entity.Property(m => m.ColoredColor)
                      .HasColumnName("colored_color")
                      .IsOptional()
                      .HasMaxLength(128)
                      .IsUnicode(true)
                      .IsVariableLength();

                entity.Property(m => m.Cut)
                      .HasColumnName("cut")
                      .IsOptional();

                entity.Property(m => m.Polish)
                      .HasColumnName("polish")
                      .IsOptional();

                entity.Property(m => m.Symmetry)
                      .HasColumnName("symmetry")
                      .IsOptional();

                entity.Property(m => m.Fluorescence)
                      .HasColumnName("fluorescence")
                      .IsOptional()
                      .HasMaxLength(128)
                      .IsUnicode(true)
                      .IsVariableLength();

                entity.Property(m => m.Comment)
                      .HasColumnName("comment")
                      .IsOptional()
                      .HasMaxLength(512)
                      .IsUnicode(true)
                      .IsVariableLength();
            }
        }

        private static void InitializeProduct<TProduct>(EntityTypeConfiguration<TProduct> entity)
            where TProduct : Product
        {
            entity.Property(m => m.Index)
                  .HasColumnName("index")
                  .IsRequired();

            entity.Property(m => m.Status)
                  .HasColumnName("status")
                  .IsRequired();

            entity.Property(m => m.Cost)
                  .HasColumnName("cost")
                  .IsRequired();

            entity.Property(m => m.SalePrice)
                  .HasColumnName("sale_price")
                  .IsRequired();

            entity.Property(m => m.UploadId)
                  .HasColumnName("upload_id")
                  .IsRequired();
        }
    }
}